package com.character.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.character.entity.MyReturn;
import com.character.entity.Question;
import com.character.entity.Tester;
import com.character.service.TesterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * (Tester)表控制层
 *
 * @author makejava
 * @since 2021-11-26 16:13:33
 */
@RestController
@RequestMapping("tester")
@Slf4j
public class TesterController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private TesterService testerService;

    /**
     * 分页查询所有数据
     *
     * @param page   分页对象
     * @param tester 查询实体
     * @return 所有数据
     */
    @GetMapping
    public MyReturn selectAll(Integer page, Integer limit, Tester tester) {
        Page<Tester> pageObj = new Page<>(page, limit);
        QueryWrapper<Tester> queryWrapper = new QueryWrapper<>();
        if(ObjectUtil.isNotEmpty(tester.getName())){
            queryWrapper.like("name",tester.getName());
        }
        if(ObjectUtil.isNotEmpty(tester.getPhone())){
            queryWrapper.like("phone",tester.getPhone());
        }
        Page<Tester> page1 = this.testerService.page(pageObj,queryWrapper);
        return super.success(page1.getRecords(),page1.getTotal());

    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public MyReturn selectOne(@PathVariable Serializable id) {
        return success(this.testerService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param tester 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public MyReturn insert(@RequestBody Tester tester) {

        QueryWrapper<Tester> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", tester.getPhone());
        List<Tester> list = this.testerService.list(wrapper);

        if (list.size() > 0) {
            return super.failed(list.get(0));
        } else {
            tester.setTestTime(new Date());
            boolean save = this.testerService.save(tester);
            if (save) {
                return super.success(tester);
            } else {
                return super.failed(tester);
            }

        }
    }

    /**
     * 修改数据
     *
     * @param tester 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    public MyReturn update(@RequestBody Tester tester) {
        return super.success(this.testerService.updateById(tester));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    public MyReturn delete(@RequestParam("idList[]") List<Long> idList) {
        return super.success(this.testerService.removeByIds(idList));
    }
}

